/*******************************************************************************
* MAIN FIGURES AND TABLES FOR "REGULATORY INCENTIVES FOR INNOVATION: 
THE FDA'S BREAKTHROUGH THERAPY DESIGNATION"
*******************************************************************************/

clear all
version 16.0
pause on
program drop _all
capture log close
set more off

*******************************************************************************
* DEFINE DIRECTORIES
*******************************************************************************

* SET GLOBAL MACRO PATH 
global main ""
cd $main

*******************************************************************************
* SET LOCAL MACROS 
*******************************************************************************

* Set BTD date 
local btd_date "09Jul2012" 

set scheme s2color

********************************************************************************
* FIGURE 2: DISTRIBUTION OF TIME-TO-MARKET 
********************************************************************************

* TIME TO MARKET 
use analytic_full_public, clear

/* Identify true non-BTD sample */
gen sampA = btd == 0 & date_sub >= td(`btd_date')
count if sampA == 1 

/* Identify true BTD sample */
gen sampB = btd == 1 & date_sub >= td(`btd_date')

local title_nda_app_day "Regulatory Review"
local title_p3_sub_day "Phase III to Regulatory Review"
local title_p2_sub_day "Phase II to Regulatory Review"

foreach outcome in p3_sub_day nda_app_day p2_sub_day {

local file `outcome'_AB
twoway ///
(kdensity `outcome' if sampB == 1, lcolor(black) lwidth(thick)) ///
(kdensity `outcome' if sampA == 1,  lcolor(black)  ///
title("`title_`outcome''", size(large)) ///
ytitle("Density", size(medium)) ///
xtitle("Days", size(medium)) ///
ylabel(, angle(horizontal) labsize(small) tlength(.75)) ///
xlabel(,labsize(small) tlength(.75)) ///
legend(label(1 "BTD") label(2 "Non-BTD") cols(2) size(medium) position(6) lcolor(black) ) ///
graphregion(color(white)))
graph save `file'.gph, replace

}

local file time_to_market 
grc1leg nda_app_day_AB.gph p3_sub_day_AB.gph p2_sub_day_AB.gph, col(2) scale(.8) xsize(10) ysize(7) legendfrom(nda_app_day_AB.gph) span graphregion(color(white)) title("Panel A. Time-to-Market")
graph save `file'.gph, replace

********************************************************************************
* TABLE 1: SUMMARY STATISTICS: UNMATCHED SAMPLE
********************************************************************************

local file table1

use analytic_full_public, clear 

* Incorporate 
merge 1:1 applno using analytic_matched, nogen keepusing(sampaW sampaY sampaX sampA sampB)

* identify pre-2012, non-BTD variables *
gen sampaWXY = 1 if sampaW == 1 | sampaY == 1 | sampaX == 1
replace sampaWXY = 0 if mi(sampaWXY)

* identify non-BTD variables *
gen sampaWXYA = 1 if sampaW == 1 | sampaY == 1 | sampaX == 1 | sampA == 1
replace sampaWXYA = 0 if mi(sampaWXYA)

* keep DID sample only 
gen treat = 1 if sampB == 1 
replace treat = 0 if mi(treat)
gen ctrl = 1 if sampaWXYA == 1
replace ctrl = 0 if mi(ctrl)

gen cancer = atc_level1 == "L"
gen metab = atc_level1 == "A"
gen antiinf = atc_level1 == "J"
gen nerv = atc_level1 == "N"

gen firm_drug_rank_h = firm_drug_rank == 2
gen firm_drug_rank_l = firm_drug_rank == 1

* convert to month-level 
foreach outcome in nda_app_day p2_sub_day p3_sub_day { 
replace `outcome' = `outcome'/(30.5)
}

matrix B = J(13,5,.)

local var_compare "nda priority fast accel bbw cancer metab antiinf nerv private nda_app_day p2_sub_day p3_sub_day"

local count = 1

foreach x in  `var_compare' {

ttest `x', by(ctrl)

matrix B[`count',1] = round(`r(mu_1)', 0.01)
matrix B[`count',2] = round(`r(sd_1)', 0.01)

matrix B[`count',3] = round(`r(mu_2)', 0.01)
matrix B[`count',4] = round(`r(sd_2)', 0.01)

matrix B[`count',5] = round(`r(p)', 0.01)

local count = `count' + 1

}

mat coln B =  "Mean" "SD" "Mean" "SD" "T-Test"
esttab matrix(B) using `draft_fd'`file'.tex, replace ///
compress coeflabels(r1 "\addlinespace \hspace{3mm} Small Molecule (0/1)" r2 "\addlinespace \hspace{3mm} Priority Review (0/1)" r3 "\addlinespace \hspace{3mm} Fast Track (0/1)" r4 "\addlinespace \hspace{3mm} Accelerated Approval (0/1)" r5 "\addlinespace \hspace{3mm} Black Box Warning (0/1)" r6 "\addlinespace \hspace{3mm} ATC: Cancer (0/1)" r7 "\addlinespace \hspace{3mm} ATC: Metabolism  (0/1)" r8 "\addlinespace \hspace{3mm} ATC: Antiinfectives (0/1)" r9 "\addlinespace \hspace{3mm} ATC: Nervous System  (0/1)" r10 "\addlinespace \hspace{3mm}Sponsor: Private Firm (0/1)" r11 "\addlinespace \hspace{3mm} Regulatory Review (Months)" r12 "\addlinespace \hspace{3mm} Phase 2 to Regulatory Review (Months)" r13 "\addlinespace \hspace{3mm} Phase 3 to Regulatory Review (Months)") noobs

********************************************************************************
* TABLE 2:  SUMMARY STATS OF MATCHED SAMPLE
********************************************************************************

local file table2

use analytic_matched_public, clear

* keep DID sample only 
keep if sampB == 1 | sampA == 1 | sampaX == 1 | sampaY == 1

gen treat = 1 if sampB == 1 | sampaY == 1
replace treat = 0 if mi(treat)
gen ctrl = 1 if sampA == 1 | sampaX == 1
replace ctrl = 0 if mi(ctrl)

gen cancer = atc_level1 == "L"
gen metab = atc_level1 == "A"
gen antiinf = atc_level1 == "J"
gen nerv = atc_level1 == "N"

gen firm_drug_rank_h = firm_drug_rank == 2
gen firm_drug_rank_l = firm_drug_rank == 1

* convert to month-level 
foreach outcome in nda_app_day p2_sub_day p3_sub_day { 
replace `outcome' = `outcome'/(30.5)
}

matrix B = J(13,5,.)

local var_compare "nda priority fast accel bbw cancer metab antiinf nerv private nda_app_day p2_sub_day p3_sub_day"

local count = 1

foreach x in  `var_compare' {

ttest `x', by(ctrl)

matrix B[`count',1] = round(`r(mu_1)', 0.01)
matrix B[`count',2] = round(`r(sd_1)', 0.01)

matrix B[`count',3] = round(`r(mu_2)', 0.01)
matrix B[`count',4] = round(`r(sd_2)', 0.01)

matrix B[`count',5] = round(`r(p)', 0.01)

local count = `count' + 1

}


mat coln B =  "Mean" "SD" "Mean" "SD" "T-Test"
esttab matrix(B) using `draft_fd'`file'.tex, replace ///
compress coeflabels(r1 "\addlinespace \hspace{3mm} Small Molecule (0/1)" r2 "\addlinespace \hspace{3mm} Priority Review (0/1)" r3 "\addlinespace \hspace{3mm} Fast Track (0/1)" r4 "\addlinespace \hspace{3mm} Accelerated Approval (0/1)" r5 "\addlinespace \hspace{3mm} Black Box Warning (0/1)" r6 "\addlinespace \hspace{3mm} ATC: Cancer (0/1)" r7 "\addlinespace \hspace{3mm} ATC: Metabolism  (0/1)" r8 "\addlinespace \hspace{3mm} ATC: Antiinfectives (0/1)" r9 "\addlinespace \hspace{3mm} ATC: Nervous System  (0/1)" r10 "\addlinespace \hspace{3mm}Sponsor: Private Firm (0/1)" r11 "\addlinespace \hspace{3mm} Regulatory Review (Months)" r12 "\addlinespace \hspace{3mm} Phase 2 to Regulatory Review (Months)" r13 "\addlinespace \hspace{3mm} Phase 3 to Regulatory Review (Months)") noobs

********************************************************************************
* TABLES 3: IMPACT ON TIME-TO-MARKET
********************************************************************************

local file table3

use analytic_matched_public, clear

keep if did_samp == 1

foreach outcome in nda_app_day p3_sub_day p2_sub_day {

xi: eststo: nbreg `outcome' btd_new post_btd_new i.year_app, vce(robust)
estadd ysumm 
estadd local ymean2 = `: display %5.2f e(ymean)'  
estadd local fda "N"
estadd local disease "N"
local loglik `: display %5.0f e(ll)' 
estadd local loglik = `loglik'
estadd local obs = `e(N)'

xi: eststo: nbreg `outcome' btd_new post_btd_new i.year_app nda priority fast accel bbw private, vce(robust)
estadd ysumm 
estadd local ymean2 = `: display %5.2f e(ymean)'  
estadd local fda "Y"
estadd local disease "N"
local loglik `: display %5.0f e(ll)' 
estadd local loglik = `loglik'
estadd local obs = `e(N)'

xi: eststo: nbreg `outcome' btd_new post_btd_new i.year_app nda priority fast accel bbw private i.atc_level1_id, vce(robust) 
estadd ysumm 
estadd local ymean2 = `: display %5.2f e(ymean)'  
estadd local fda "Y"
estadd local disease "Y"
local loglik `: display %5.0f e(ll)' 
estadd local loglik = `loglik'
estadd local obs = `e(N)'
}


esttab _all using `file'.tex, ///
b(3)  ///
replace se  ///
constant nomtitles  ///
keep(btd_new post_btd_new nda priority private)  ///
m label nonotes compress nodepvars wrap star(* 0.10 ** 0.05 *** 0.001)  ///
stats(ymean2 fda disease obs  loglik, labels("Mean"  "Controls: Drug Characteristics" "Controls: Disease" "Observations" "log likelihood")) ///
nonotes
eststo clear 


********************************************************************************
* TABLE 5: FIRM EXPERIENCE 
********************************************************************************

local file table5

use analytic_matched_public, clear

keep if did_samp == 1 

foreach outcome in nda_app_day p3_sub_day p2_sub_day {
	
xi: eststo: nbreg `outcome' btd_new post_btd_new i.year_app nda priority fast accel bbw private i.atc_level1_id if firm_drug_rank == 1, vce(robust)
estadd ysumm 
estadd local ymean2 = `: display %5.2f e(ymean)'  
estadd local fda "Y"
estadd local disease "Y"
estadd local obs = `e(N)'
estadd local loglik = `: display %5.0f  e(ll)'

xi: eststo: nbreg `outcome' btd_new post_btd_new i.year_app nda priority fast accel bbw private i.atc_level1_id if firm_drug_rank == 2, vce(robust)
estadd ysumm 
estadd local ymean2 = `: display %5.2f e(ymean)'  
estadd local fda "Y"
estadd local disease "Y"
estadd local obs = `e(N)'
estadd local loglik = `: display %5.0f  e(ll)'
}

esttab _all using `file'.tex, ///
b(3)  ///
replace se  ///
constant nomtitles  ///
keep(btd_new post_btd_new nda priority private)  ///
m label nonotes compress nodepvars wrap star(* 0.10 ** 0.05 *** 0.001)  ///
stats(ymean2 fda disease obs loglik, labels("Mean"  "Controls: Drug Characteristics" "Controls: Disease" "Observations" "log likelihood")) ///
nonotes
eststo clear 

********************************************************************************
* TABLE 6: MECHANISM - TRIAL CHARACTERISTICS 
********************************************************************************

 capture program drop reg_trialcharac
 program define reg_trialcharac
	
	keep if did_samp == 1
	
	xi: eststo: `2' `3' btd_new post_btd_new i.year_app nda priority fast accel bbw private i.atc_level1_id, vce(robust) 
	estadd ysumm
	estadd local ymean2 = `: display %5.2f e(ymean)' 
	estadd local obs = `e(N)'
	estadd local r2_2 = `: display %5.3f e(r2)' 
	
end

 capture program drop reg_trialcharac_out
 program define reg_trialcharac_out

	esttab _all using `1'.tex, ///
	b(3) replace se  ///
	constant  ///
	keep(btd_new post_btd_new nda priority private)  ///
	m label nonotes compress nodepvars wrap star(* 0.10 ** 0.05 *** 0.001)  ///
	stats(ymean2 obs r2_2, labels("Mean" "Observations" "\$R^2$")) ///
	mtitle("Enrollment" "Number of Facilities" "Number of Arms" "Randomized (0/1)" "Mask (Double) (0/1)" ) ///
	nonotes
	eststo clear 
	
end

* PHASE 3 
use analytic_matched_public, clear

local charac "number_of_facilities enrollment "

foreach outcome in `charac' {
reg_trialcharac nom reg p3_`outcome' 

}

local charac "number_of_arms random maskdoub"

foreach outcome in `charac' {
reg_trialcharac nom reg p3_`outcome' 
}

reg_trialcharac_out table6_a

* PHASE 2 
use analytic_matched_public, clear

local charac "enrollment number_of_facilities"

foreach outcome in `charac' {
reg_trialcharac ln reg p2_`outcome' 

}

local regtype reg  
local outtype nom 

local charac "number_of_arms random maskdoub"

foreach outcome in `charac' {
reg_trialcharac nom reg p2_`outcome' 
}

reg_trialcharac_out table6_b


SSS
